package chenji.StrongFossil.Inventory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import chenji.StrongFossil.Fossil.PuncherFossil.PunchFossil;

@SuppressWarnings("unused")
public class PunchManager {
	ItemStack Puncher;

	private final String FinalMessage = "&c&l&m       &6&l&m      &a&l&m      &b&l&m      &c&l&m       ".replaceAll("&",
			"§");
	private final String FirstMessage = "&c&l&m      &6&l&m      §c强化信息&b&l&m      &c&l&m      ".replaceAll("&", "§");
	private HashMap<Player, PunchInventory> PunchInventoryList = new HashMap<Player, PunchInventory>();
	private HashMap<Material, Integer> PunchList;
	private HashMap<Material, Double> LevelChance;
	private HashMap<Material, Double> BrokeStartChance;
	private HashMap<Material, Double> BrokeLevelAdd;
	private HashMap<Material, Integer> DurableDrop=new HashMap<Material,Integer>();
	private int DefaultDrop=10;
	private boolean EnableBroken;
	private boolean EnableChance;
	private double DefaultChance;
	private String EmptyPunchName;
	private boolean ArrowOtherLore;
	private List<String> LimitLoreList;

	/**
	 * 获取默认成功率
	 * 
	 * @param m
	 * @param nowPunch
	 * @return
	 */
	public double getFinalChange(Material m, int nowPunch) {
		if (!(this.LevelChance.containsKey(m))) {
			return DefaultChance;
		}
		double chance = this.LevelChance.get(m);
		return DefaultChance - (chance * nowPunch);
	}

	public double getFinalChange(PunchFossil pf, int nowPunch, Material m) {
		double d = getFinalChange(m, nowPunch);
		if (d == DefaultChance) {
			return DefaultChance;
		}
		d = d + pf.getValue();
		if (d >= DefaultChance) {
			return DefaultChance;
		} else {
			return d;
		}
	}

	public double getBrokeChange(int nowPunch, Material m) {
		if(!(this.BrokeStartChance.containsKey(m))){return 0;}
		if(this.BrokeStartChance.get(m)>nowPunch){return 0;}
		Double i = this.BrokeStartChance.get(m);
		Double ia = this.BrokeLevelAdd.get(m);
		i=i+ia*(nowPunch-i);
		return i;
	}

	public int getDurableDrop(Material m, int nowPunch, PunchFossil pf) {
		if (!(this.DurableDrop.containsKey(m))) {
			return this.DefaultDrop;
		}
		int i = this.DurableDrop.get(m);
		if (pf == null) {
			return i;
		} else {
			return (int) (i - pf.getValue());
		}

	}
	public ItemStack Punch(ItemStack i) {
			ItemMeta m = i.getItemMeta();
			ArrayList<String> lore;
			if (m.hasLore()) {
				lore = (ArrayList<String>) m.getLore();
			} else {
				lore = new ArrayList<String>();
				lore.add(this.FirstMessage);

				lore.add(this.FinalMessage);

			}
			lore.remove(lore.size() - 1);
			lore.add(this.EmptyPunchName);
			lore.add(this.FinalMessage);
			m.setLore(lore);
			i.setItemMeta(m);
			return i;
	}
	/**
	 * 构造函数
	 * 
	 * @param punchList
	 * @param levelChance
	 * @param brokeStartChance
	 * @param brokeLevelAdd
	 */
	public PunchManager(HashMap<Material, Integer> punchList, HashMap<Material, Double> levelChance,
			HashMap<Material, Double> brokeStartChance, HashMap<Material, Double> brokeLevelAdd) {
		this.PunchList = punchList;
		this.LevelChance = levelChance;
		this.BrokeStartChance = brokeStartChance;
		this.BrokeLevelAdd = brokeLevelAdd;
	}

	/**
	 * 载入更多参数
	 * 
	 * @param enableBroken
	 * @param enableChance
	 * @param defaultChance
	 * @param emptyPunchName
	 * @param arrowOtherLore
	 * @param limitLoreList
	 */
	public void install(boolean enableBroken, boolean enableChance, double defaultChance, String emptyPunchName,
			boolean arrowOtherLore, List<String> limitLoreList) {
		this.EnableChance = enableChance;
		this.EnableBroken = enableBroken;
		this.DefaultChance = defaultChance;
		this.EmptyPunchName = emptyPunchName;
		this.ArrowOtherLore = arrowOtherLore;
		this.LimitLoreList = limitLoreList;
	}

	/**
	 * 向HashMap里注册PunchInventory
	 * 
	 * @param p
	 * @param punchInventory
	 */
	public void registerPunchInventory(Player p, PunchInventory punchInventory) {
		PunchInventoryList.put(p, punchInventory);
	}

	/**
	 * 清理资源，回收资源。
	 * 
	 * @param punchInventory
	 */
	public void close(PunchInventory punchInventory) {
		if (this.PunchInventoryList.containsKey(punchInventory.getPlayer())) {
			punchInventory.DropItem();
			this.PunchInventoryList.remove(punchInventory.getPlayer());
			
		}
	}

	/**
	 * 用Player获取PunchInventory实例，如果没有则返回null。
	 * 
	 * @param p
	 * @return
	 */
	public PunchInventory getPunchInventory(Player p) {
		if (this.PunchInventoryList.containsKey(p)) {
			return this.PunchInventoryList.get(p);
			
		}
		return null;
	}

	/**
	 * 获取一个材质是否可以打孔。
	 * 
	 * @param type
	 * @return
	 */
	public boolean canPunch(Material type) {
		System.out.println(this.PunchList);
		return this.PunchList.containsKey(type);
	}

	/**
	 * 检测当前被打孔的数量
	 */
	public int getPunchNumber(ItemStack i) {
		if (i == null) {
			return 0;
		}
		if (!(i.hasItemMeta())) {
			return 0;
		}
		if (!(i.getItemMeta().hasLore())) {
			return 0;
		}
		List<String> lore = i.getItemMeta().getLore();
		int startindex = 0, endindex = 0, nowindex = 0;
		for (String s : lore) {
			if (s.equals(this.FirstMessage)) {
				startindex = nowindex;
				continue;
			}
			if (s.equals(this.FinalMessage)) {
				endindex = nowindex;
				continue;
			}
			nowindex++;
		}
		return (endindex - startindex);
	}

	public int getEmptyPunchNumber(ItemStack i) {
		if (i == null) {
			return 0;
		}
		if (!(i.hasItemMeta())) {
			return 0;
		}
		if (!(i.getItemMeta().hasLore())) {
			return 0;
		}
		List<String> lore = i.getItemMeta().getLore();
		int startindex = 0, endindex = 0, nowindex = 0, notEmptyindex = 0;
		for (String s : lore) {
			if (s.equals(this.FirstMessage)) {
				startindex = nowindex;
			}
			if (s.equals(this.FinalMessage)) {
				endindex = nowindex;
			}
			nowindex++;
		}
		nowindex = 0;
		for (String s : lore) {
			if (nowindex < endindex && nowindex > startindex) {
				if (!(s.equalsIgnoreCase(this.EmptyPunchName))) {
					notEmptyindex++;
				}
			}
			nowindex++;
		}
		return notEmptyindex;
	}

	/**
	 * 获取一个物品类型最大允许打孔的数量
	 * 
	 * @param m
	 * @return
	 */
	public int getMaxPunchNumber(Material m) {
		if (this.PunchList.containsKey(m)) {
			return this.PunchList.get(m);
		}
		return 0;
	}

	/**
	 * 获取玩家是否已经打开了一个PunchInventory
	 * 
	 * @param p
	 * @return
	 */
	public boolean containPunchInventory(Player p) {
		return this.PunchInventoryList.containsKey(p);
	}

	/**
	 * 手动刷新玩家已经打开的PunchInventory
	 * 
	 * @param p
	 */
	public void refreshPunchInventory(Player p) {
		if (this.PunchInventoryList.containsKey(p)) {
			this.PunchInventoryList.get(p).refreshState();
		}
	}

	public String getFirstSubject() {
		return this.FirstMessage;
	}

	public String getFinalSubject() {
		return this.FinalMessage;
	}
}
